﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;

namespace POSSystem.BLL
{
    public class ProductDetails : _ProductDetails
    {
        public enum ProductStatus
        {
            Purchased = 1,
            Saled
        }

        public static ProductDetails GetAllProductDetails()
        {
            try
            {
                ProductDetails p = new ProductDetails();
                p.Where.Deleted.Value = false;
                p.Where.BranchID.Value = Branches.CurrentBranchID;
                p.Query.Load();

                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("ProductDetails.cs", "GetAllProductDetails", e.StackTrace);
                throw e;
            }
        }

        public static bool IsStockEmpty(int stockID)
        {
            try
            {
                ProductDetails p = new ProductDetails();
                p.Where.Deleted.Value = false;
                p.Where.StockLocationID.Value = stockID;
                p.Where.ProductStatusID.Value = ProductStatus.Purchased;
                p.Query.Top = 1;
                p.Query.Load();

                return p.RowCount <= 0;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("ProductDetails.cs", "GetAllProductDetails", e.StackTrace);
                throw e;
            }
        }

        public static bool IsIMEIAvailable(String IMEI)
        {
            try
            {
                ProductDetails prod = new ProductDetails();
                prod.Where.IMEI.Value = IMEI;
                prod.Where.ProductStatusID.Value = ProductStatus.Purchased;
                prod.Where.BranchID.Value = Branches.CurrentBranchID;
                prod.Query.Load();

                return prod.RowCount > 0;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ProductDetails.cs", "GetAllProductDetails", ex.StackTrace);
                throw ex;
            }
        }

        public static ProductDetails GetByIMEI(String IMEI)
        {
            try
            {
                ProductDetails prod = new ProductDetails();
                prod.Where.IMEI.Value = IMEI;
                prod.Where.ProductStatusID.Value = ProductStatus.Purchased;
                prod.Where.BranchID.Value = Branches.CurrentBranchID;
                prod.Query.Load();

                return prod;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ProductDetails.cs", "GetAllProductDetails", ex.StackTrace);
                throw ex;
            }
        }

        public static ProductDetails GetByIMEI(String IMEI, int branchID)
        {
            try
            {
                ProductDetails prod = new ProductDetails();
                prod.Where.IMEI.Value = IMEI;
                prod.Where.BranchID.Value = branchID;
                prod.Query.Load();

                return prod;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ProductDetails.cs", "GetAllProductDetails", ex.StackTrace);
                throw ex;
            }
        }

        public static ProductDetails GetProductDetailByIMEIs(String IMEIs)
        {
            try
            {
                string[] str = IMEIs.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string val = "";
                for (int i = 0; i < str.Length; i++)
                    val += "'" + str[i] + "',";
                if(val.Length > 0)
                    val = val.Remove(val.Length - 1);

                ProductDetails prod = new ProductDetails();
                prod.Where.IMEI.Value = val;
                prod.Where.IMEI.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                prod.Where.BranchID.Value = Branches.CurrentBranchID;
                prod.Query.Load();

                return prod;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ProductDetails.cs", "GetAllProductDetails", ex.StackTrace);
                throw ex;
            }
        }

        public void AddInfo(int productID, String description, String IMEI, int supplierID, int stockLocationID, int statusID )
        {
            this.ProductID = productID;
            this.Description = description;
            this.IMEI = IMEI;
            this.SupplierID = supplierID;
            //this.StockLocationID = stockLocationID;
            this.ProductStatusID = statusID;
            this.StoreDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
            this.Deleted = false;
            this.BranchID = Branches.CurrentBranchID;
        }
    }
}
